package com.zmcn.msg.business.msgGroup.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.zmcn.msg.bean.po.Msg;
import com.zmcn.msg.bean.po.MsgGroup;
import com.zmcn.msg.bean.vo.MsgGroupVO;
import com.zmcn.msg.bean.vo.MsgVO;
import com.zmcn.msg.fw.action.Page;
import com.zmcn.msg.fw.dao.BaseDao;


/**
 * 
 * 上传消息批次信息数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class MsgGroupDao extends BaseDao {
	/**
	 * 获取上传消息批次信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<MsgGroupVO> getList(Page<MsgGroupVO> page) {
		String hqlResult = "select new " + MsgGroupVO.class.getName() + MsgGroupVO.STRUCT;
		String hqlCount = "select count(msggroup)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(MsgGroup.class.getName()).append(" msggroup");
		sb.append(" where msggroup.status=0");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		for (String key : paras.keySet()) {
			if ("uploador".equals(key)||"receiver".equals(key)) {
				sb.append(" and msggroup.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");				
			} 
		}
		sb.append(" order by msggroup.uploadTime desc");
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}	
	
	/**
	 * 根据ID获取上传消息批次信息对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<MsgVO> getMsgGroupVO(Page<MsgVO> page,String batchId) {
		String hqlResult = "select new " + MsgVO.class.getName() + MsgVO.STRUCT;
		String hqlCount = "select count(msg)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Msg.class.getName()).append(" msg");
		sb.append(" where msg.batchId=?");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		ps.add(batchId);
		for (String key : paras.keySet()) {
			if ("content".equals(key)) {
				sb.append(" and msg.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			} 
		}
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}
}
